package com.yuhuofei2021.spring.boot.weather.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yuhuofei2021.spring.boot.weather.entity.City;
import com.yuhuofei2021.spring.boot.weather.service.WeatherReportService;

/**
 * @description Weather Report控制器
 * @author yuhuofei2021
 * @date 2021年9月21日
 */
@RestController
@RequestMapping("/report")
public class WeatherReportController {
	
	@Resource
	private	WeatherReportService weatherReportService;
	
	private final static Logger logger = LoggerFactory.getLogger(WeatherReportController.class);
	
	@GetMapping("/cityId/{cityId}")
	public ModelAndView getReportByCityId(@PathVariable String cityId, Model model) throws Exception {
		//TODO 改由城市数据API子服务提供数据
		
		List<City> cityList = null;
		try {
			//TODO 改由城市数据API子服务来提供城市数据
			cityList = new ArrayList<>();
			City city = new City();
			city.setCityId("101280601");
			city.setCityName("深圳");
			cityList.add(city);
		} catch (Exception e) {
			logger.error("获取城市列表出错！",e);
		}
		model.addAttribute("title", "天气预报");
		model.addAttribute("cityId", cityId);
		model.addAttribute("cityList", cityList);
		model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
		return new ModelAndView("weather/report","reportModel",model);
	}
}
